package dyyx.drools.demo2;

import java.io.StringReader;
import java.util.Collection;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;

import dyyx.Utils;

public class Demo2Main {

	public static void main(String[] args) throws Exception {
	
		String drl = Utils.readFromClassPath("drools/demo2.drl", null);
		
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)),ResourceType.DRL);
		Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		kbase.addKnowledgePackages(pkgs);
		StatelessKnowledgeSession session = kbase.newStatelessKnowledgeSession();
			
        Product product = new Product();	
        // 调用 update 后会重新触发 otherRule 执行 ，otherRule 两次都匹配
        product.setDiscount(1.5);
        
		session.execute(product);
		
		System.out.println("****");
		// 第一次 otherRule 不匹配 ， 更新完discount 后 ，重新触发 otherRule 执行 
        product.setDiscount(0.5);
		session.execute(product);

	}
}
